package cn.xiaomubiao.oschine.fragment.News_fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.xiaomubiao.oschine.MyApplication;
import cn.xiaomubiao.oschine.R;
import cn.xiaomubiao.oschine.activity.MyInfoBaseActivity;
import cn.xiaomubiao.oschine.adapter.news_adapter.NewsAdapter;
import cn.xiaomubiao.oschine.base.BaseEachFragment;
import cn.xiaomubiao.oschine.base.BaseRecyclerViewAdapter;
import cn.xiaomubiao.oschine.bean.News;
import cn.xiaomubiao.oschine.bean.NewsList;
import cn.xiaomubiao.oschine.util.HttpUtil;
import cn.xiaomubiao.oschine.util.SpUtils;
import cn.xiaomubiao.oschine.util.ToastUtils;

/**
 * Created by ywz on 2017/4/2.
 * 综合模块 -- 热点页签
 */

public class Content2 extends BaseEachFragment {

    List<News> mStringList = new ArrayList<>();
    private int mI;
    private HttpUtil<NewsList> mHttpUtil;
    private XRecyclerView xrv;
    private NewsAdapter mAdapter;

    //布局
    @Override
    public View loadSuccess() {

        View inflate = View.inflate(getContext(), R.layout.news_xrecyview, null);
        xrv = (XRecyclerView) inflate.findViewById(R.id.news_xrcyview);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        xrv.setLayoutManager(manager);

        //适配器
        mAdapter = new NewsAdapter(mStringList);
        xrv.setAdapter(mAdapter);

        initEvent();

        return inflate;

    }

    private void initEvent() {

        //条目点击事件的监听 -- web页面跳转
        mAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnRecycleViewItemClickListener() {
            @Override
            public void onClick(View view, int position) {
                position = position - 1;
                News value = mStringList.get(position);
                int pagerId = value.getId();

                SpUtils.putBoolean(getContext(), "isLinsent" + pagerId, true);
                mAdapter.notifyDataSetChanged();

                // 获取要传递到详情进行url拼接的id
                int id = value.getId();
                // 构建意图对象,存放要进行传递的数据
                Intent intent = new Intent(getActivity(), MyInfoBaseActivity.class);
                intent.putExtra("id", id);
                intent.putExtra("title", "资讯详情");
                intent.putExtra("className", NewsDetailFragment.class.getName());
                intent.putExtra("imgId", R.drawable.ic_brows_refresh);

                // 开启意图
                startActivity(intent);

            }
        });

        //开启刷新
        xrv.setPullRefreshEnabled(true);
        xrv.setPullRefreshEnabled(true);


        //刷新更多数据的监听 -- 加载更多
        xrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            //下啦刷新
            @Override
            public void onRefresh() {
                MyApplication.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 弹个吐司
                        Toast.makeText(getContext(), "刷新成功!", Toast.LENGTH_SHORT).show();
                        // 刷新完成
                        xrv.refreshComplete();

                    }
                }, 2000);
            }

            //加载更多
            @Override
            public void onLoadMore() {
                MyApplication.mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //重新请求数据,改变url
                        ++mI;
                        if (mI < 4) {
                            String url = "http://10.0.2.2:8080/oschina/list/hotspot/page" + mI + ".xml";
                            mHttpUtil = new HttpUtil<>(getActivity(), NewsList.class);
                            mHttpUtil.getStringData(url, new HttpUtil.OnGetDataListener<NewsList>() {
                                @Override
                                public void onSuccess(NewsList newsList) {
                                    // ( 服务端问题,多个判断,防止空指针异常
                                    if (newsList != null) {
                                        List<News> data = newsList.getList();
                                        // 将新数据添加到旧集合中
                                        mStringList.addAll(data);

                                    }
                                    // 加载完成
                                    mActivity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            xrv.loadMoreComplete();
                                            ToastUtils.showToastShort(getContext(), "加载完成");

                                        }
                                    });



                                }

                                @Override
                                public void onFailure(Exception e) {
                                    // 加载更多时出现网络错误
                                    mActivity.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            xrv.loadMoreComplete();

                                        }
                                    });

                                    ToastUtils.showToastOnUiShort(mActivity,"网络出现了问题,请检查后进行加载");

                                }
                            });

                        } else {
                            Toast.makeText(getContext(), "暂无更多,休息一下吧!", Toast.LENGTH_SHORT).show();
                            xrv.setNoMore(true);

                        }

                    }

                }, 1500);
            }
        });

    }

    //加载数据
    @Override
    public void loadData() {
        mI = 0;
        mHttpUtil = new HttpUtil<>(getActivity(), NewsList.class);
        String url = "http://10.0.2.2:8080/oschina/list/hotspot/page" + mI + ".xml";
        mHttpUtil.getStringData(url, new HttpUtil.OnGetDataListener<NewsList>() {
            @Override
            public void onSuccess(NewsList newsList) {
                List<News> list = newsList.getList();
                mStringList = list;
                mCommonPager.showSuccessPager(mStringList);
            }

            @Override
            public void onFailure(Exception e) {
                mCommonPager.showFailPager();
            }
        });
    }

}
